﻿namespace Compass.Maui.Services.FinishGoods;
//Pallet发生变更时更新
public class PalletUpdateEventArgs : EventArgs
{
    public PalletUpdateEventArgs(PalletDto updatedPalletDto)
    {
        UpdatedPalletDto=updatedPalletDto;
    }
    public PalletDto UpdatedPalletDto { get;}//指明谁发生了变化
}

public interface IPalletService:IBaseService<PalletDto>
{
    Task<List<PalletDto>> GetPalletsByPackingListIdAsync(Guid packingListId);
    event EventHandler<PalletUpdateEventArgs> Update;
    Task UpdateWithEventAsync(Guid id, PalletDto palletDto);
    Task UpdateStatusesWithEventAsync(Guid id, PalletDto palletDto);
}
public class PalletService: BaseService<PalletDto>, IPalletService
{
    private const string ServiceName = "Pallet";
    private readonly IHttpRestClient _client;
    public PalletService(IHttpRestClient client):base(client,ServiceName)
    {
        _client = client;
    }


    public async Task<List<PalletDto>> GetPalletsByPackingListIdAsync(Guid packingListId)
    {
        var request = new BaseRequest
        {
            Method = Method.Get,
            Route = $"{ServiceName}/PackingList/{packingListId}",
        };
        var response = await _client.ExecuteAsync<List<PalletDto>>(request);
        return response.Status ? response.Result : new List<PalletDto>();
    }


    public event EventHandler<PalletUpdateEventArgs>? Update;
    public async Task UpdateWithEventAsync(Guid id,PalletDto palletDto)
    {
        await UpdateAsync(id, palletDto);
        //触发事件
        Update?.Invoke(this,new PalletUpdateEventArgs(palletDto));
    }

    public async Task UpdateStatusesWithEventAsync(Guid id, PalletDto palletDto)
    {
        var request = new BaseRequest
        {
            Method = Method.Put,
            Route = $"{ServiceName}/UpdateStatuses/{id}",
            Param = palletDto
        };
        var response = await _client.ExecuteAsync<PalletDto>(request);
        //触发事件
        Update?.Invoke(this, new PalletUpdateEventArgs(palletDto));
    }
}